﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SilverlightCommands;
using System.ServiceModel;
using KMT_Pro.Web;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;

namespace KMT_Pro.ViewModels.ClientViewModels
{
    public class ClientEditInfoViewModel : MessageSendableViewModel
    {
        CustomerContext _context;

        public string companyID = string.Empty;
        public string companyName = string.Empty;
        public string companygubun = string.Empty;
        public string address = string.Empty;
        public string etc = string.Empty;
        public bool isReadOnly = true;
        public string gubun;

        public ClientEditInfoViewModel()
        {
            
        }

        #region -------------------------------------수정버튼 클릭---------------------------------------
        /// <summary>
        /// 수정버튼 클릭
        /// </summary>
        public ICommand editClick
        {
            get { return new RelayCommand(p => EditbtnClick2(p)); }
        }

        public void EditbtnClick2(object param)
        {
            if (this.IsReadOnly)
                gubun = "U"; //수정
            else
                gubun = "I"; //신규

            try
            {
                this.IsBusy = true;

                _context = new CustomerContext();

                var q = _context.Load(_context.ClientMaster_InsertQuery(this.CompanyID.Trim()
                                                                      , this.CompanyName.Trim()
                                                                      , this.Address.Trim()
                                                                      , this.Companygubun.Trim()
                                                                      , DateTime.Now.ToString()
                                                                      , this.Etc.Trim()
                                                                      , gubun));
                q.Completed += EditComplete; //비동기 완료 이벤트 등록
            }
            catch (Exception ex)
            {
                this.IsBusy = false;
                ChildWindow errorWin = new ErrorWindow(ex);
                errorWin.Show();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void EditComplete(object sender, EventArgs e)
        {
            LoadOperation lo = sender as LoadOperation;
            try
            {
                foreach (ResultMessage GAP in lo.AllEntities)
                {
                    MessageBox.Show(GAP.ExceptionMessage);
                    this.IsBusy = false;
                }
            }
            catch (Exception)
            { this.IsBusy = false; }
        }
        #endregion -------------------------------------수정버튼 클릭 end---------------------------------------

        public bool IsReadOnly
        {
            set
            {
                this.isReadOnly = value;
                this.OnPropertyChanged("IsReadOnly");
            }
            get
            {
                return isReadOnly;
            }
        }

        public string CompanyID
        {
            set
            {
                this.companyID = value;
                this.OnPropertyChanged("CompanyID");
            }
            get
            {
                return companyID;
            }
        }

        public string CompanyName
        {
            set
            {
                this.companyName = value;
                this.OnPropertyChanged("CompanyName");
            }
            get
            {
                return companyName;
            }
        }

        public string Companygubun
        {
            set
            {
                this.companygubun = value;
                this.OnPropertyChanged("Companygubun");
            }
            get
            {
                return companygubun;
            }
        }

        public string Address
        {
            set
            {
                this.address = value;
                this.OnPropertyChanged("Address");
            }
            get
            {
                return address;
            }
        }

        public string Etc
        {
            set
            {
                this.etc = value;
                this.OnPropertyChanged("Etc");
            }
            get
            {
                return etc;
            }
        }
    }
}
